Skip to content

[Core][BugFix] Fix thread safety issue in RequestOutputCollector #22576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

22quinn
Copy link
Collaborator

@22quinn 22quinn commented Aug 9, 2025

Essential Elements of an Effective PR Description Checklist

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

Purpose

We need to safely set asyncio.Event() from a different thread otherwise it could lead to hang issue in some cases where await self.ready.wait() remains blocked although the event was set.

Test Plan

Tested with a vendor setup.

Test Result

Tested with a vendor setup.

(Optional) Documentation Update

Signed-off-by: 22quinn <[email protected]>
Copy link

github-actions bot commented Aug 9, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the v1 label Aug 9, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses a critical thread-safety issue in RequestOutputCollector. By using loop.call_soon_threadsafe(), it ensures that the asyncio.Event is set safely from a different thread, preventing potential deadlocks. This is the standard and correct approach for such cross-thread communication with asyncio. The change is well-implemented and effectively resolves the race condition.

@22quinn 22quinn requested a review from houseroad August 9, 2025 18:58
@22quinn 22quinn marked this pull request as ready for review August 9, 2025 18:58
@22quinn 22quinn added the ready ONLY add when PR is ready to merge/full CI is needed label Aug 9, 2025
@robertgshaw2-redhat
Copy link
Collaborator

robertgshaw2-redhat commented Aug 9, 2025

When does this need to be called from a separate thread? There is only one thread in the AsyncLLM IIUC

@22quinn
Copy link
Collaborator Author

22quinn commented Aug 10, 2025

When does this need to be called from a separate thread? There is only one thread in the AsyncLLM IIUC

Seems some nuance in our complex setup caused this and it's consistently reproducible. I've been trying to construct a small repro but not able yet

@njhill
Copy link
Member

njhill commented Aug 10, 2025

@22quinn yes this should not be required. It would only be needed if the put method was called from outside the asyncio event loop, but that should never be the case. This is the same for similar asyncio utilities such as asyncio.Queue.

@22quinn
Copy link
Collaborator Author

22quinn commented Aug 18, 2025

On a high level, the issue is we initialize AsyncLLM from one thread, then we have a thread pool executor which dynamically call generate from different threads. I was not able to minimize the setup, but it seems a valid use case - @robertgshaw2-redhat @njhill do you think it's fine to apply this fix? I think using call_soon_threadsafe has no downside even if it's called from the same thread.
cc @xw285cornell

@njhill
Copy link
Member

njhill commented Aug 18, 2025

@22quinn I still don't think this is the right thing to do. You should definitely not be calling AsyncLLM.generate from different threads. It's an async method which should be only be called from the (single) asyncio event loop thread.

If you have code to run multiple generates in parallel, it should use parallel async tasks rather than threads. If you want to share your client code I can help to suggest how it should be changed.

There has been some discussion about supporting multi-threaded non-async use with the LLM interface (I think it "kind of" works already), which would be the correct way to go if you really want to use a threadpool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants